<?php

namespace app\common\service\person;

use app\admin\model\store\GoodsCouponIssueUser;
use app\common\service\BaseService;
use think\facade\Cache;

class MyCouponService extends BaseService
{


    /**
     * Author:Yingbo Sun
     * 优惠卷列表
     * @param $uid
     * @param int $status
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function coupon($uid, $status)
    {
        if (empty($status)) {
            $status = 0;
        }
        if (empty($uid)) {
            return self::fail('参数错误');
        }
        $redis = Cache::store('redis');
        $redis->select(12);
        $bool = $redis->has("couponList:uid:{$uid}:status:{$status}");
        if ($bool) {
            $data = $redis->get("couponList:uid:{$uid}:status:{$status}");
        } else {
            $where['uid'] = $uid;
            $where['status'] = $status;
            $data = GoodsCouponIssueUser::where($where)->with('coupon')->select();
            $data && $data = $data->toArray();
            $redis->set("couponList:uid:{$uid}:status:{$status}", $data);
        }
        return self::success('获取成功', $data);
    }


    public static function couponByOrder($uid)
    {
        $where['uid'] = $uid;
        $data = GoodsCouponIssueUser::where($where)->with('coupon')->select();
        $data && $data = $data->toArray();
        return self::success('获取成功', $data);
    }
}
